﻿using mds.BaseFrameModel;
using mds.Common;
using mds.ServiceInterface;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace mds.CMS
{
    public class CMSProvider 
    {
        public GridPage<PageTemplate> GetTemplatePage(int currentPage, int pageSize, string searchKey)
        {
            ArgumentHelper.AssertInRange("currentPage", currentPage, 1, int.MaxValue);
            ArgumentHelper.AssertInRange("pageSize", pageSize, 1, int.MaxValue);
            ArgumentHelper.AssertNotEmpty(searchKey);
            int total = 0;
            var list=PageTemplateDao.GetPageList(currentPage,  pageSize,  searchKey, out total);
            var result = new GridPage<PageTemplate>(total, pageSize, list);
            return result;
        }

        public ServiceInterface.GridPage<WebInfo> GetInfoPage(int currentPage, int pageSize, string searchKey)
        {
            ArgumentHelper.AssertInRange("currentPage", currentPage, 1, int.MaxValue);
            ArgumentHelper.AssertInRange("pageSize", pageSize, 1, int.MaxValue);
            ArgumentHelper.AssertNotEmpty(searchKey);
            int total = 0;
            var list = WebInfoDao.GetPageList(currentPage, pageSize, searchKey, out total);
            var result = new GridPage<WebInfo>(total, pageSize, list);
            return result;
        }

        public OperationResult<int> WebInfoUpdate(int infoId, int infoType, string title, string tag, string infoContent, string author, DateTime publicDate,int templateId, string localPath, string fileName, int createBy, int modifyBy)
        {
            ArgumentHelper.AssertInRange("infoId", infoId, 0, int.MaxValue);
            ArgumentHelper.AssertNotEmpty(title);
            ArgumentHelper.AssertNotEmpty(tag);
            ArgumentHelper.AssertNotEmpty(infoContent);           
            var id=WebInfoDao.WebInfoUpdate(infoId, infoType, title, tag, infoContent, author, publicDate, createBy, modifyBy);
            if (id > 0)
            {
                if (!string.IsNullOrEmpty(localPath))
                {
                    string fullPath;
                    if(string.IsNullOrEmpty(fileName))
                        fullPath=localPath+"/"+DateTimeHelper.GetTimeName()+".html";
                    else
                         fullPath=localPath+"/"+fileName;
                    if (templateId > 0)
                    {
                        string tempTemplate = PageTemplateDao.GetTemplateById(templateId);
                        Dictionary<string, string> dicLabel = GetDicLabels(title, tag, infoContent, author, publicDate);
                        if (File.Exists(fullPath))
                        {
                            File.Delete(fullPath);
                        }
                        File.AppendAllText(fullPath, LabelProvider.Instance.GetContent(tempTemplate, dicLabel));
                    }
                }
            }
            OperationResult<int> result = new OperationResult<int>((id > 0) ? id : infoId, (id > 0) ? true : false);
            return result;
        }

        private Dictionary<string, string> GetDicLabels(string title, string tag, string infoContent, string author, DateTime publicDate)
        {
            Dictionary<string, string> temp = new Dictionary<string, string>();
            temp.Add("Title", title);
            temp.Add("Tag", tag);
            temp.Add("InfoContent", infoContent);
            temp.Add("Author", author);
            temp.Add("PublicDate", publicDate.ToShortDateString());
            return temp;
        }

        public OperationResult<int> PageTemplateUpdate(int pageTemplateId, string templateName, string templateContent, int templateType, int createBy, int modifyBy)
        {
            ArgumentHelper.AssertInRange("pageTemplateId", pageTemplateId, 0, int.MaxValue);
            ArgumentHelper.AssertNotEmpty(templateName);
            ArgumentHelper.AssertNotEmpty(templateContent);
            var id = PageTemplateDao.PageTemplateUpdate(pageTemplateId, templateName, templateContent,templateType, createBy, modifyBy);
            OperationResult<int> result = new OperationResult<int>((id > 0) ? id : pageTemplateId, (id > 0) ? true : false);
            return result;
        }

        public OperationResult<bool> DeleteInfos(int[] infoIds, int operationBy)
        {
            ArgumentHelper.AssertNotNull<int[]>(infoIds);
            bool blnResult=WebInfoDao.DeleteInfos(infoIds,operationBy);
            OperationResult<bool> result = new OperationResult<bool>(blnResult);
            return result;
        }

        public OperationResult<bool> DeleteTemplates(int[] templateIds, int operationBy)
        {
            ArgumentHelper.AssertNotNull<int[]>(templateIds);
            bool blnResult = PageTemplateDao.DeleteInfos(templateIds, operationBy);
            OperationResult<bool> result = new OperationResult<bool>(blnResult);
            return result;
        }
    }
}
